package com.yl.util;

import sun.misc.Unsafe;

import java.lang.reflect.Field;

/**
 * @author wlf
 * @since 2022/8/9
 */
public class JdkUtil {

    /**
     * 忽略非法反射警告，适用于jdk11
     * 或者配置【VM options】java --illegal-access=deny --add-opens java.base/java.lang=ALL-UNNAMED -jar xxx.jar
     */
    public static void disableWarning() {
        try {
            Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe");
            theUnsafe.setAccessible(true);
            Unsafe u = (Unsafe) theUnsafe.get(null);
            Class cls = Class.forName("jdk.internal.module.IllegalAccessLogger");
            Field logger = cls.getDeclaredField("logger");
            u.putObjectVolatile(cls, u.staticFieldOffset(logger), null);
        } catch (Exception e) {
            // ignore
        }
    }

}
